package com.tycmc.signelectroniccontracts.tools.update;

import android.Manifest;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.tools.base.ActBase;
import com.tycmc.signelectroniccontracts.tools.manager.AppInfo;
import com.tycmc.signelectroniccontracts.tools.net.tools.NetUtils;
import com.tycmc.signelectroniccontracts.tools.permissions.PermissionsTool;
import com.tycmc.signelectroniccontracts.tools.show.AlertDialog;
import com.tycmc.signelectroniccontracts.tools.show.LogShow;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;

import java.io.File;


/**
 * Created by GY on 2018/1/20.
 */

public class UpDateTool {
    public final static int NOT_NET_WORKE = 1;//无网络
    public final static int NOT_WRITE_STRONG = 2;//无存储权限
    public static Handler upDataHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            switch (message.what) {
                case NOT_NET_WORKE:
                    ToastShow.popupToast(actBase, actBase.getString(R.string.no_network));
                    break;
                case NOT_WRITE_STRONG:
                    break;
            }
            return true;
        }
    });
    private static ActBase actBase;

    /**
     * 提示新版本更新
     *
     * @param actBase
     * @param memeo   更新内容
     * @param downurl 下载链接
     */
    public static void downloadUrl(final ActBase actBase, String memeo, final String downurl) {
        downloadUrl(actBase, memeo, downurl, null);
    }

    public static void downloadUrl(final ActBase actBase, String memeo, final String downurl, View.OnClickListener onClickListener) {
        UpDateTool.actBase = actBase;
        AlertDialog alertDialog = new AlertDialog(actBase).builder().setTitle(actBase.getString(R.string.updata_title))
                .setMsg(memeo).setPositiveButton(actBase.getString(R.string.updata), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        setDownloadUrl(actBase, downurl);
                    }
                });
        if (onClickListener == null) {
            alertDialog.show();
        } else {
            alertDialog.setNegativeButton(actBase.getString(R.string.cancel), onClickListener).show();
        }
    }


    /**
     * 添加下载路径
     *
     * @param actBase Activity对象
     * @param downUrl 下载路径
     */
    private static void setDownloadUrl(final ActBase actBase, final String downUrl) {
        if (NetUtils.networkTest(actBase)) {
            PermissionsTool.requestPermiss( new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionsTool.PerMissionCallBack() {
                @Override
                public void permissIsPass(String[] permission, boolean isPass) {
                    if (isPass) {
                        DownloadTool.upData(actBase, downUrl);
                    } else {
                        upDataHandler.sendEmptyMessage(NOT_WRITE_STRONG);

                    }
                }
            }, actBase);

        } else {
            upDataHandler.sendEmptyMessage(NOT_NET_WORKE);
        }
    }

    public static void getApkInfo(final ActBase actBase, final String apkPath) {
        if(!new File(apkPath).exists()){
            return;
        }
        PermissionsTool.requestPermiss(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionsTool.PerMissionCallBack() {
            @Override
            public void permissIsPass(String[] permission, boolean isPass) {
                if(isPass){
                    PackageManager pm = actBase.getPackageManager();
                    PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
                    if (info != null) {
                        ApplicationInfo appInfo = info.applicationInfo;
                        String packageName = appInfo.packageName;//得到安装包名称
                        String version = info.versionName;//获取安装包的版本号
                        int versionCode = info.versionCode;//获取安装包的内置版本号
                        LogShow.i("安装包名称:"+packageName+"\n版本号 "+version+"\nversionCode "+versionCode);
                    }else {
                        LogShow.i("null");
                    }
                }
            }
        },actBase);


    }

    /**
     * 安装本地apk 不用重新下载
     * @param actBase
     * @param apkPath
     */
    public static void installApk(final ActBase actBase, final String apkPath){
        if(!new File(apkPath).exists()){
            return;
        }
        PermissionsTool.requestPermiss(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionsTool.PerMissionCallBack() {
            @Override
            public void permissIsPass(String[] permission, boolean isPass) {
                if(isPass){
                    PackageManager pm = actBase.getPackageManager();
                    PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
                    if (info != null) {
                        ApplicationInfo appInfo = info.applicationInfo;
                        String packageName = appInfo.packageName;//得到安装包名称
                        String version = info.versionName;//获取安装包的版本号
                        int versionCode = info.versionCode;//获取安装包的内置版本号
                        if(packageName.equals(actBase.getPackageName())&&versionCode> AppInfo.getVersionCode(actBase)){
                            new AlertDialog(actBase).builder().setMsg(actBase.getString(R.string.updata_title)).setPositiveButton(actBase.getString(R.string.updata), new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DownloadTool.installApk(actBase,new File(apkPath));
                                }
                            }).setNegativeButton(actBase.getString(R.string.cancel),null).show();
                        }

                    }else {
                        LogShow.i("null");
                    }
                }
            }
        },actBase);
    }
}
